package com.rabbit.orderservice.controller;


import com.rabbit.orderservice.model.OrderInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/send")
    public String send() {
        String uuid= UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("order-exchange","order_queue",
                new Date()+", 订单ID ：" + uuid);
        return "success";
    }

    // 传一个对象: 当我们什么都不处理时，会优先报错：类型不对，此时我们可以将 对象类序列化即可，但是又出现个问题： 传输的内容太复杂（一大串数字）
    @RequestMapping("/send2")
    public String send2() {
        String uuid= UUID.randomUUID().toString();
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(uuid);
        orderInfo.setOrderDate(new Date());
        rabbitTemplate.convertAndSend("order-exchange","order_queue",
                orderInfo);
        return "success";
    }

    // 如果类 不实现序列化 的话，我们可以对 rabbitTemplate 添加个参数，报错的原因就是因为这个内容的载荷 类型不对(Payload)
    // 报错信息： SimpleMessageConverter only supports String, byte[] and Serializable payloads
    @RequestMapping("/send3")
    public String send3() {
        String uuid= UUID.randomUUID().toString();
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(uuid);
        orderInfo.setOrderDate(new Date());
        rabbitTemplate.convertAndSend("order-exchange","order_queue",
                orderInfo);
        return "success";
    }
}
